/*
*  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
*  reserved.
*
*  Redistribution and use in source and binary forms, with or withouta
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*  notice, this list of conditions and the following disclaimer.
*
*  2. Redistributions in binary form must reproduce the above copyright
*  notice, this list of conditions and the following discalimer in
*  the documentation and/or other materials provided with the
*  distribution.
*
*  3. The end-user documentation included with the redistribution,
*  if any, must include the following acknowledgment:
*  "This product includes software developed by the
*  Sun Microsystems, Inc. for Project JXTA."
*  Alternately, this acknowledgment may appear in the software itself,
*  if and wherever such third-party acknowledgments normally appear.
*
*  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
*  must not be used to endorse or promote products derived from this
*  software without prior written permission. For written
*  permission, please contact Project JXTA at http://www.jxta.org.
*
*  5. Products derived from this software may not be called "JXTA",
*  nor may "JXTA" appear in their name, without prior written
*  permission of Sun.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*  ====================================================================
*
*  This software consists of voluntary contributions made by many
*  individuals on behalf of Project JXTA.  For more
*  information on Project JXTA, please see
*  <http://www.jxta.org/>.
*
*
*  $Id: Resources.java,v 1.10 2007/01/26 21:54:32 bondolo Exp $
*/
package net.jxta.myjxta.util;

import net.jxta.ext.config.Resource;
import net.jxta.ext.config.ResourceNotFoundException;
import net.jxta.logging.Logging;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.HashMap;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * The class  that gives out the resources for images, colors, etc.
 * 
 * @version $Id: Resources.java,v 1.10 2007/01/26 21:54:32 bondolo Exp $
 *
 * @author james todd [gonzo at jxta dot org]
 * @author mike mcangus [mcangus at jxta dot org]
 */

public final class Resources
extends HashMap<String,Object> {
    
    /** The instance of the resources to use */
    public static Resources instance = null;
    
    /** Colors that are used for several definitions */
    private  static final Color PURPLE = new Color(102, 102, 153);
    private  static final Color LIGHT_YELLOW = new Color(255, 255, 204);
    private  static final Color LIGHT_BLUE = new Color(224, 224, 235);
    private  static final Color LIGHT_GREEN = new Color(120, 255, 120);
    private static final String IMAGE_PREFIX = "net/jxta/myjxta/resources/images/";

    /** The image to use is a desired image was not found */
    private static final String IMAGE_NOT_FOUND = IMAGE_PREFIX + "imageNotFound.jpg";
    private static final Logger LOG = Logger.getLogger(Resources.class.getName());
    
    private static ResourceBundle strings;
    
    static {
        Constants c = Constants.getInstance();
        String sr = c.get(Constants.STRING_RESOURCES);
        
        if (sr != null) {
            try {
                strings = ResourceBundle.getBundle(sr);
            } catch (MissingResourceException mre) {
                if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                    LOG.log(Level.SEVERE, "Caught unexpected Exception", mre);
                }
                strings=null;
            }
        } else {
            strings=null;
        }
        
        if (strings == null) {
            if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
                LOG.info("can\'t find user specified resource: " + sr);
            }
            
            Resource r = new Resource();
            
            try {
                r.load(Resources.class.getResourceAsStream(Env.CONSTANTS));
            } catch (ResourceNotFoundException rnfe) {
                if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                    LOG.log(Level.SEVERE, "Caught unexpected Exception", rnfe);
                }
            }
            
            sr = r.get(Constants.STRING_RESOURCES);
            
            if (sr != null) {
                try {
                    strings = ResourceBundle.getBundle(sr);
                } catch (MissingResourceException mre) {
                    if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                        LOG.log(Level.SEVERE, "Caught unexpected Exception", mre);
                    }
                }
            }
        }
    }
    
    /**
     * Get the instance of Resources to use 
     * 
     * @return the application wide instance of Resources 
     */
    public static synchronized Resources getInstance() {
        if (instance == null) {
            instance = new Resources();
        }

        return instance;
    }

    /**
     * Get a resource as an Icon 
     *
     * @param resource the resource to retrieve
     * @return the resource as an icon or null if the resource 
     *         was not found
     */
    public ImageIcon getIconResource(String resource) {
        return (ImageIcon)get(resource);
    }

    /**
     * Get a resource as a Color
     *
     * @param resource the resource to retrieve
     * @return the resource as Color object  or null if the resource 
     *         was not found
     */
    public Color getColorResource(String resource) {
        return (Color)get(resource);
    }

    /**
     * Get a resource as a Font
     *
     * @param resource the resource to retrieve
     * @return the resource as Font object  or null if the resource 
     *         was not found
     */
    public Font getFontResource(String resource) {
        return (Font)get(resource);
    }

    /**
     * Get a resource as a string
     * 
     * @param resource the resource to retrieve
     * @return the resource as String object  or null if the resource 
     *         was not found
     */
    public String getStringResource(String resource) {
        return (String)get(resource);
    }

    /**
     * Get a resource as a Integer object
     * 
     * @param resource the resource to retrieve
     * @return the resource as Integer object  or null if the resource 
     *         was not found
     */
    public Integer getIntResource(String resource) {
        return (Integer)get(resource);
    }

    /**
     * @return the strings ResourceBundle.
     */
    public static ResourceBundle getStrings() {
        return strings;
    }
    
    /**
     * Create a new Resources instance, which loads all relevant
     * resources.
     */
    private  Resources() {
        // put the desired colors in the map
        put("Application.Background",PURPLE);
        put("Application.Foreground",Color.WHITE);
        put("Application.TableHeader",LIGHT_YELLOW);
        put("Dialog.Background",LIGHT_BLUE);
        put("Dialog.Button",LIGHT_GREEN);
        put("TabPane.Selected",Color.BLACK);
        put("TabPane.Unselected",Color.GREEN);

        // put application images
        put("Application.Icon", getIcon(IMAGE_PREFIX + "icon.gif"));

        // put the images for the JxtaTableTree
        put("TreeTable.PeerPresent", getIcon(IMAGE_PREFIX + "user.png"));//"PeerPresent.png"));
        put("TreeTable.PeerAbsent", getIcon(IMAGE_PREFIX + "user.png"));//"PeerAbsent.png"));
        put("TreeTable.PeerGroupJoined", getIcon(IMAGE_PREFIX + "user_group.png"));//"PeerGroup.png"));
        put("TreeTable.PeerGroup", getIcon(IMAGE_PREFIX + "user_group.png"));//"GroupNotJoined.png"));
        put("TreeTable.GroupContainer", getIcon(IMAGE_PREFIX + "user_group.png"));//"GroupContainer.png"));
        put("TreeTable.Share", getIcon(IMAGE_PREFIX + "filenew.png"));
        put("Dialog.Secure", getIcon(IMAGE_PREFIX + "secure.png"));
        put("Dialog.UnSecure", getIcon(IMAGE_PREFIX + "unsecure.png"));
        put("TicTacToe.X", getIcon(IMAGE_PREFIX + "tictactoex.png"));
        put("TicTacToe.O", getIcon(IMAGE_PREFIX + "tictactoeo.png"));
        put("TicTacToe.BLANK", getIcon(IMAGE_PREFIX + "tictactoeblank.png"));
        
        put("Welcome", getIcon(IMAGE_PREFIX + "welcome.png"));

        put("GroupTable.connectedToRdv", getIcon(IMAGE_PREFIX + "GreenCircle.gif"));
        put("GroupTable.notConnectedToRdv", getIcon(IMAGE_PREFIX + "RedCircle.gif"));
        put("GroupTable.isRdv", getIcon(IMAGE_PREFIX + "BlueCircle.gif"));

        

	// put padding for tree images
        put("TreeTable.padding",new Integer(1));

        put("badge.sun", getIcon(IMAGE_PREFIX + "logo_sun_small.gif"));
    }

    /**
     * Return an  ImageIcon given a path to an image
     * 
     * @param resource the path to the image
     * @return the corresponding ImageIcon
     */
    private  static ImageIcon getIcon(String resource) {
        ClassLoader cl = Resources.class.getClassLoader();
        URL r = cl.getResource(resource);

        return new ImageIcon(r != null ?
                             r : cl.getResource(IMAGE_NOT_FOUND));
    }
}
